<?php
// +----------------------------------------------------------------------
// | Bwsaas
// +----------------------------------------------------------------------
// | Copyright (c) 2015~2020 http://www.buwangyun.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Gitee ( https://gitee.com/buwangyun/bwsaas )
// +----------------------------------------------------------------------
// | Author: buwangyun <hnlg666@163.com>
// +----------------------------------------------------------------------
// | Date: 2020-9-28 10:55:00
// +----------------------------------------------------------------------

namespace app\common\model;

use buwang\base\BaseModel;

/**
 * 成员应用表
 * Class MemberMiniapp
 * @package app\manage\model
 */
class MemberMiniapp extends BaseModel
{
    //protected $table = 'bw_member_miniapp';// 表名
    // 模型初始化
    protected static function init()
    {
        //TODO:初始化内容
    }

    /**
     * 关联应用
     *
     * @return void
     */
    public function miniapp()
    {
        return $this->hasOne('Miniapp', 'id', 'miniapp_id');
    }

    /**
     * 应用后台所属管理员
     * @return void
     */
    public function member()
    {
        return $this->hasOne('Member', 'id', 'member_id');
    }

    /**
     * 应用绑定的用户端口创始人
     * @return void
     */
    public function user()
    {
        return $this->hasOne('User', 'id', 'member_id');
    }

    /**
     * 用户购买的应用
     * @return void
     */
    public function order()
    {
        return $this->hasOne('MemberMiniappOrder', 'id', 'miniapp_order_id');
    }

    /**
     * 用户小程序列表
     */
    public static function lists(array $param = [], $pages = [])
    {
        return self::view('member_miniapp', '*')
            ->view('member', 'phone_id', 'member.id = member_miniapp.member_id')
            ->view('miniapp', 'types,miniapp_dir,is_wechat_pay,is_alipay_pay,is_lock as miniapp_lack,is_openapp,template_id', 'miniapp.id = member_miniapp.miniapp_id')
            ->view('member_miniapp_order', 'update_var', 'member_miniapp.miniapp_order_id = member_miniapp_order.id')
            ->where($param)
            ->where(['miniapp.is_lock' => 0])
            ->order('id desc')
            ->paginate(20, false, ['query' => $pages]);
    }

    /**
     * 后台添加编辑
     * @param array $param 数组
     */
    public static function edit(array $param)
    {
        $data['member_id'] = $param['member_id'];
        $data['appname'] = $param['appname'];
        $data['update_time'] = time();
        if (isset($param['id']) && $param['id'] > 0) {
            return self::where('id', $param['id'])->update($data);
        } else {
            $data['miniapp_id'] = $param['miniapp_id'];
            $data['miniapp_order_id'] = $param['miniapp_order_id'];
            $data['create_time'] = time();
            $last_id = self::insertGetId($data);
            return self::where('id', $last_id)->update(['service_id' => uuid(3, true, $last_id)]);
        }
    }

    /**
     * 用户添加编辑
     * @param array $param 数组
     */
    public static function editer(array $param)
    {
        $data = Util::array_remove_empty($param);
        $data['update_time'] = time();
        return self::where('id', $param['id'])->update($data);
    }

    /**
     * 锁定用户
     * @param integer $id
     */
    public static function lock(int $id)
    {
        $result = self::where(['id' => $id])->find();
        $result->is_lock = $result->is_lock ? 0 : 1;
        if ($result->is_lock == 0) {
            $member = Member::where(['id' => $result->member_id])->field('is_lock')->find();
            if ($member->is_lock == 1) {
                return false;
            }
        }
        return $result->save();
    }
}
